package com.orifine.sql.simple;



/**
 * 分页
 * @author http://www.bowos.com
 *
 */
public class SimplePage {
	/**
	 * @param page 当前页码
	 * @param listMax 每页记录数
	 */
	public SimplePage(Integer listMax, Integer page){
		if(listMax != null){
			this.listMax = listMax;
		} else {
			listMax = 10;
		}
		if(page != null){
			this.page = page;
		} else {
			page = 1;
		}
	}
	
	/**
	 * @param count 当前页总记录数
	 * @param page 当前页码
	 * @param listMax 每页记录数
	 */
	public SimplePage(Integer count, Integer listMax, Integer page){
		if(listMax != null){
			this.listMax = listMax;
		} else {
			this.listMax = 10;
		}
		if(page != null){
			this.page = page;
		} else {
			this.page = 1;
		}
		init(count);
	}
	
	public void init(Integer count){
		try{
			if(count%listMax == 0){
				pageCount = count/listMax;
			} else {
				pageCount = count/listMax + 1;
			}
		} catch (Exception e){
			pageCount = 0;
		}
		try {
			if (page > pageCount || page < 1) {
				page = 1;
			}
		} catch (Exception e) {
			page = 1;
		}
		listFirstIndex = (this.getPage() - 1) * this.getListMax();
		listLastIndex = this.getPage() * this.getListMax() - 1;
	}
	
	/**
	 * 当前页码
	 */
	private Integer page = 1;
	
	/**
	 * 总页数
	 */
	private Integer pageCount;
	
	/**
	 * 当前页第一条记录索引
	 */
	private Integer listFirstIndex;
	
	/**
	 * 当前页最后一条记录索引
	 */
	private Integer listLastIndex;
	
	/**
	 * 每页记录数
	 */
	private Integer listMax = 10;
	
	public SimpleParam getCurrentPageParam(){
		return getPageParam(getPage());
	}
	
	public SimpleParam getNextPageParam(){
		return getPageParam(getNextPage());
	}
	
	public SimpleParam getPrevPageParam(){
		return getPageParam(getPrevPage());
	}
	
	public SimpleParam getFirstPageParam(){
		return getPageParam(getFirstPage());
	}
	
	public SimpleParam getLastPageParam(){
		return getPageParam(getLastPage());
	}
	
	private SimpleParam getPageParam(Integer page){
		SimpleParam bowosParam = new SimpleParam();
		bowosParam.setParam("listMax",getListMax());
		bowosParam.setParam("page",page);
		return bowosParam;
	}
	
	public SimplePageBar getPageBar(Integer size){
		return new SimplePageBar(page, pageCount, size);
	}
	
	/**
	 * 获取上一页
	 */
	public Integer getPrevPage(){
		if(page - 1 < 1){
			return 1;
		} else {
			return page - 1;
		}
	}
	
	/**
	 * 获取第一页
	 */
	public Integer getFirstPage(){
		return 1;
	}
	
	/**
	 * 获取下一页
	 */
	public Integer getNextPage(){
		if(page + 1 > pageCount){
			return getLastPage();
		} else {
			return page + 1;
		}
	}
	
	/**
	 * 获取最后一页
	 */
	public Integer getLastPage(){
		return pageCount;
	}
	
	/**
	 * 获取当前页
	 */
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getPageCount() {
		return pageCount;
	}
	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}
	public Integer getListFirstIndex() {
		return listFirstIndex;
	}
	public void setListFirstIndex(Integer listFirstIndex) {
		this.listFirstIndex = listFirstIndex;
	}
	public Integer getListLastIndex() {
		if(listLastIndex == null){
			return 0;
		}
		return listLastIndex;
	}
	public void setListLastIndex(Integer listLastIndex) {
		this.listLastIndex = listLastIndex;
	}
	public Integer getListMax() {
		if(listMax == null){
			return 10;
		}
		return listMax;
	}
	public void setListMax(Integer listMax) {
		this.listMax = listMax;
	}
}
